/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: message_sender.h
 *
 * Purpose: a message sender define and implementation
 *
 * Developer:
 *   wen.gu , 2023-08-01
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __XBUS_TRANSPORT_MESSAGE_SENDER_H__
#define __XBUS_TRANSPORT_MESSAGE_SENDER_H__

#include "xbus/core/xmessage.h"
#include "xbus/transport/message_worker.h"
#include "xbus/transport/xmessage_queue.h"

namespace xbus::transport {

class MessageSender: public MessageWorker<XMessageQueue> {
public:
    using DataFillHandler = std::function<XErrC(uint8_t* payload_buf, uint32_t size)>;
public:
    MessageSender(const std::string& name, int32_t priority) : MessageWorker<XMessageQueue>(name, priority) { }
    virtual ~MessageSender() = default;

public:
    virtual XErrC flush(int32_t timeout_ms = core::WaitType::WaitForever){ //wait for all message in sender to be sent.
    if (msg_queue_ == nullptr) { return XErrC::InvalidStatus;}
    if ((timeout_ms == core::WaitType::NoWait)) { return msg_queue_->empty() ? XErrC::OK : XErrC::Undefined; }
    else if (timeout_ms == core::WaitType::WaitForever){
        while (1) {
            if (msg_queue_->empty()) { return XErrC::OK; }
            osal::SleepMs(1);            
        }        
    } else {
        for (size_t i = 0; i < timeout_ms; i++) {  
            if (msg_queue_->empty()) { return XErrC::OK; }
            osal::SleepMs(1);
        }            
    }    

    return XErrC::TimeOut;
    }

public:
    virtual XErrC send(core::XMessagePtr msg_ptr) {
        return (msg_queue_ != nullptr) ? msg_queue_->push(msg_ptr) : XErrC::InvalidStatus;
    }
    virtual XErrC send(uint8_t type, const uint8_t* payload_data, uint32_t payload_size) {
        auto msg_ptr = core::XMessage::CreateMsg(type, payload_size + XMSG_HDR_LENGTH(0));
        if (msg_ptr == nullptr) {
            return XErrC::Undefined;
        }

        if (!msg_ptr->append_payload(payload_data, payload_size)) { return XErrC::Undefined; }

        return send(msg_ptr);
    }

    /** this API will alloc message with 'payload_size' and call 'fill_handler' method to fill payload to message */
    virtual XErrC send(uint8_t type, uint32_t payload_size, DataFillHandler& fill_handler) {
        auto msg_ptr = core::XMessage::CreateMsg(type, payload_size + XMSG_HDR_LENGTH(0));
        if (msg_ptr == nullptr) {
            return XErrC::Undefined;
        }

        if (!msg_ptr->assign(payload_size)) { return XErrC::Undefined; }

        if (fill_handler) { //todo refine me??
            XErrC err = fill_handler(msg_ptr->payload(), msg_ptr->payload_size());
            if (err != XErrC::OK) {
                return err;
            }        
        }

        return send(msg_ptr);
    }
};


} //namespace xbus::transport


#endif /** !__XBUS_TRANSPORT_MESSAGE_SENDER_H__ */